package com.chaoxing.controller;


import com.chaoxing.bean.VO.BookVO;
import com.chaoxing.bean.VO.PageVO;
import com.chaoxing.service.BookService;
import com.chaoxing.bean.DO.Book;
import com.chaoxing.bean.DTO.Result;
import com.chaoxing.bean.DTO.ResultTable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import springfox.documentation.annotations.ApiIgnore;

@Controller
@Api(tags = "后台接口模块")
public class IndexController {


    @Autowired
    private BookService bookService ;



    @RequestMapping("/")
    @ApiIgnore
    public String  index() {
        return "index" ;
    }


    @RequestMapping("/index2")
    @ApiIgnore
    public String  index2() {
        return "index2" ;
    }



    @PostMapping("/insertBook")
    @ApiOperation("新增书本名称和作者")
    @ResponseBody
    public String  insertBook(@RequestBody BookVO bookVO) {
        return bookService.insertBook(bookVO);
    }

    @RequestMapping("/list")
    @ApiIgnore
    public String  list() {
        return "list" ;
    }




    @PostMapping("/bookList")
    @ApiOperation("书本列表")
    @ResponseBody
    public ResultTable bookList(Integer page  , Integer limit) {



          PageVO pageVO  = new PageVO() ;
        pageVO.setPageNum(page);
        pageVO.setPageSize(limit);
        return bookService.bookList(pageVO);
    }



    @RequestMapping("/dataList")
    @ApiIgnore
    public String  dataList() {
        return "dataList" ;
    }


    @RequestMapping("/detail")
    @ApiIgnore
    public ModelAndView detail(@RequestParam(value = "id") String id) {
        ModelAndView modelAndView = new ModelAndView() ;
        Book book =  bookService.getBookDetail(id) ;
        modelAndView.addObject("book",book);
        modelAndView.setViewName("detail");
        return  modelAndView;
    }


    @GetMapping("/deleteBook")
    @ApiOperation("删除书本")
    @ResponseBody
    public Result deleteBook(@RequestParam(value = "id") String id) {
        return bookService.deleteBook(id);
    }



}
